#!/usr/bin/perl
#save_create.cgi

require './diskman-lib_soft.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my ($level, $disklist, $volname, $needspare, $sparedisk);

$level = 1;
$disklist = $in{'listinput'};
$volname = $in{'volname'};
#$volname = "volume1";

my (@rdinfo, @dinfo, @free_volumes, @free_disks, $i);

#@rdinfo =  get_check_raid_info();
#@dinfo = get_diskinfo(\@rdinfo);

#@free_volumes = &get_free_volumes(\@rdinfo);
#@free_disks = &get_free_disks(\@dinfo);


my (@diskname, $dn, $errdisk);
@diskname = split(/\s+/, $disklist);
my ($sparedev,$index);

$sparedev = 0;

my (@rdevs);
foreach $dn (@diskname) {
	push(@rdevs, $dn);
}

#$err_msg = @rdevs;
#goto ERROR_OUT;

if (@rdevs != 2) {
	$err_msg = "$text{'err_not_two_diskarray'}";
	goto ERROR_OUT;
}

# check finished
my ($mddev);

$volname =~ /\S+(\d+)/;
$mddev = $1 + $hwinfo{'uraidstart'} - 1;
$mddev = '/dev/md'.$mddev;

foreach (@rdevs) {
                $rmpv = `/sbin/lvm pvremove $_ -ff -y 2>/dev/null`;
                $rc1 = $?>>8;
                if($rc1){
                        $err_msg = $text{'err_pvremove'};
                        goto ERROR_OUT;
                }   
}
my $rdisklist = join(" ", sort(@rdevs));

$err_msg = "$rdisklist|$mddev";

#goto ERROR_OUT;

my ($rc);
$rc = create_volume($level, $rdisklist, $volname, $mddev, $sparedev);
if ($rc) {
	$log_ev = $text{'log_ev_crtvolume_err'};
	$log_msg= &text('log_msg_crtvolume_err', $volname, 'RAID '.$level);
	nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

	#$err_msg = $rc.$text{'err_crtvol'};
	#goto ERROR_OUT;
}

$log_ev = $text{'log_ev_crtvolume_ok'};
$log_msg= &text('log_msg_crtvolume_ok', $volname, 'RAID '.$level);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);
#$out = `/sbin/lvm pvcreate $mddev`;
&redirect("index.cgi");
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
#&redirect("");


